<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @cancel="handleCancel"
    cancelText="关闭">
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button @click="handleOk" type="primary">提交</a-button>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-table ref="table" size="middle" :scroll="{x:true}" rowKey="id" :columns="columns"
               :dataSource="dataSource" :loading="loading" class="j-table-force-nowrap">
        <template slot="paramValue" slot-scope="text,record">
          <a-input :width='100' v-model="record.destTableField"/>
        </template>
        <template slot="shuru" slot-scope="text,record">
          ->
        </template>
      </a-table>
    </a-spin>

  </j-modal>
</template>

<script>
import { fieldMap,updateBatchField } from '@/api/apiDs'

export default {
  name: '任务sql字段映射',
  data() {
    return {
      confirmLoading: false,
      loading: false,
      dataSource: [],
      title: '数据映射',
      visible: false,
      width: 500,
      columns: [
        {
          title: '原数据字段',
          align: 'center',
          dataIndex: 'sourceTableField',
          ellipsis: true
        },
        {
          title: '->',
          align: 'center',
          dataIndex: 'shuru',
          ellipsis: true,
          scopedSlots: { customRender: 'shuru' }
        },
        {
          title: '数仓字段',
          align: 'center',
          dataIndex: 'destTableField',
          ellipsis: true,
          scopedSlots: { customRender: 'paramValue' }
        }]
    }
  },
  methods: {
    showTable(tid) {
      this.visible = true
      let param = { tid: tid }
      fieldMap(param).then(res => {
        if (res.success) {
          let datas = res.result
          this.dataSource = datas
        } else {
          this.$message.warning(res.message)
          this.visible = true
        }
      })

    },

    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk(){
      let param = { collectOdsFieldMaps: this.dataSource }
      updateBatchField(param).then((res) => {
        if(res.result){
          this.$message.success(res.message);
          this.$emit('ok');
        }else{
          this.$message.warning(res.message);
        }
        this.close()
      }).catch((err) => {
        this.$message.warning(err)
      })

    }
  }
}
</script>